﻿using System.Windows;
using Prism.Events;
using Prism.Regions;
using SoonCode.FocusTest.Events;
using SoonCode.FocusTest.Models;

namespace SoonCode.FocusTest.Views
{
    /// <summary>
    /// Student.xaml 的交互逻辑
    /// </summary>
    public partial class StudentWindow : BaseWindow
    {
        private IRegionManager regionManager;
        private IEventAggregator aggregator;
        private static readonly string regionName = "studentRegion";
        private FullScreenView fullScreen;
        public StudentWindow(IEventAggregator aggregator)
        {
            this.regionManager = (new RegionManager()).CreateRegionManager();
            this.aggregator = aggregator;
            RegionManager.SetRegionManager(this, this.regionManager);
            regionManager.RegisterViewWithRegion(regionName, typeof(Waiting));
            regionManager.RegisterViewWithRegion(regionName, typeof(Prepare));
            regionManager.RegisterViewWithRegion(regionName, typeof(AnswerItemA));
            regionManager.RegisterViewWithRegion(regionName, typeof(AnswerItemB));
            regionManager.RegisterViewWithRegion(regionName, typeof(AnswerItemC));
            regionManager.RegisterViewWithRegion(regionName, typeof(AnswerItemD));
            regionManager.RegisterViewWithRegion(regionName, typeof(AnswerItemE));
            regionManager.RegisterViewWithRegion(regionName, typeof(AnswerItemF));
            regionManager.RegisterViewWithRegion(regionName, typeof(AnswerItemG));
            regionManager.RegisterViewWithRegion(regionName, typeof(AnswerItemH));
            InitializeComponent();
            this.Loaded += StudentWindow_Loaded;
            this.Closed += StudentWindow_Closed;
            this.aggregator.GetEvent<OpenStudentFullScreenEvent>().Subscribe(ShowFullScreen);
        }

        private void ShowFullScreen(string name)
        {
            if (fullScreen == null)
            {
                fullScreen = new FullScreenView(aggregator);
                fullScreen.Owner=this;
            }

            fullScreen.Show(name);
        }

        private void StudentWindow_Closed(object? sender, EventArgs e)
        {
            this.regionManager.Regions.Remove(regionName);
        }

        private void NextTest(int p)
        {
            switch (p)
            {
                case 0:
                    this.regionManager.RequestNavigate(regionName, nameof(AnswerItemA));
                    break;
                case 1:
                    this.regionManager.RequestNavigate(regionName,nameof(AnswerItemB));
                    break;
                case 2:
                    this.regionManager.RequestNavigate(regionName, nameof(AnswerItemC));
                    break;
                case 3:
                    this.regionManager.RequestNavigate(regionName, nameof(AnswerItemD));
                    break;
                case 4:
                    this.regionManager.RequestNavigate(regionName, nameof(AnswerItemE));
                    break;
                case 5:
                    this.regionManager.RequestNavigate(regionName, nameof(AnswerItemF));
                    break;
                case 6:
                    this.regionManager.RequestNavigate(regionName, nameof(AnswerItemG));
                    break;
                case 7:
                    this.regionManager.RequestNavigate(regionName, nameof(AnswerItemH));
                    break;
            }
            
        }
        private void NavToPage(string pageName)
        {
            regionManager.Regions[regionName].RequestNavigate(pageName);
        }

        private void StartTest()
        {
            MemberInfo member = GlobalModel.Instance.MemberInfo;
            Task.Run(() =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    NextTest(member.testItem);
                });
                
            });
            
        }
        private void StudentWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.aggregator.GetEvent<PrepareTestEvent>().Subscribe(() => NavToPage(nameof(Prepare)));
            this.aggregator.GetEvent<CloseTestEvent>().Subscribe(() => NavToPage(nameof(Waiting)));
            this.aggregator.GetEvent<StartTestEvent>().Subscribe(StartTest);
            this.aggregator.GetEvent<EndTestEvent>().Subscribe(() => NavToPage(nameof(Waiting)));
            this.aggregator.GetEvent<ChangeTestEvent>().Subscribe(NextTest);
        }

        private void BtnClose_OnClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
